﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kira.Compiler.Bods
{
    [XmlElementMapping("DITransformCall", "name", "SQL")]
    public class DISqlQuery : BodsTransform
    {
        public IEnumerable<DIElement> Columns
        {
            get
            {
                return this.GetElement("DISchema").GetElements<DIElement>();
            }
        }

        public String OutputName
        {
            get
            {
                return this.GetElement("DISchema").GetAttrbuteValue("name");
            }
        }


        public override IEnumerable<string> InputNames
        {
            get { return new String[] { }; }
        }

        public override IEnumerable<string> OutputNames
        {
            get { return new String[] { this.OutputName }; }
        }

        public SQLText SQLText
        {
            get
            {
                var ele = this.GetElement("DIAttributes").GetElement(c => c.ElementName == "DIAttribute" && c.GetAttrbuteValue("name") == "sql_transform_configurations").GetElement("SQLTexts");
                var ele2 = ele.GetElement<SQLText>();
                return ele2;
            }
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(GetComments("SQL"));
            sb.Append("WITH ").Append(OutputName).AppendLine(" AS (").AppendLine(SQLText.Script).AppendLine(")");
            return sb.ToString();
        }
    }

    [XmlElementMapping("SQLText")]
    public class SQLText : BodsElementBase
    {
        public String DatabaseType
        {
            get
            {
                return this.GetAttrbuteValue("database_type");
            }
        }

        public String DatabaseVersion
        {
            get
            {
                return this.GetAttrbuteValue("database_version");
            }
        }

        public String Script
        {
            get
            {
                return this.GetElement("sql_text").Value;
            }
        }


    }
}
